const express = require('express');
const router = express.Router();
const sessionManager = require('../utils/sessionManager');

// 获取所有会话列表
router.get('/list', async (req, res) => {
    try {
        const sessions = await sessionManager.getAllSessions();
        res.json({
            code: 200,
            message: '获取会话列表成功',
            data: sessions
        });
    } catch (error) {
        console.error('获取会话列表失败:', error);
        res.status(500).json({
            code: 500,
            message: '获取会话列表失败',
            error: error.message
        });
    }
});

// 注销指定会话
router.delete('/:token', async (req, res) => {
    try {
        const { token } = req.params;
        const result = await sessionManager.invalidateSession(token);
        if (result) {
            res.json({
                code: 200,
                message: '注销会话成功'
            });
        } else {
            res.status(404).json({
                code: 404,
                message: '会话不存在'
            });
        }
    } catch (error) {
        console.error('注销会话失败:', error);
        res.status(500).json({
            code: 500,
            message: '注销会话失败',
            error: error.message
        });
    }
});


module.exports = router; 